package com.example.italkapp2.controller.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.italkapp2.R;
import com.example.italkapp2.model.Model;
import com.example.italkapp2.model.bean.UserInfo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

//登录页面
public class LoginActivity extends Activity {

    private EditText et_login_name;
    private EditText et_login_pwd;
    private Button bt_login_register;
    private Button bt_login_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        intiViews();
        intiListener();
    }

    private void intiViews(){
        et_login_name=findViewById(R.id.et_login_name);
        et_login_pwd=findViewById(R.id.et_login_pwd);
        bt_login_register=findViewById(R.id.bt_login_register);
        bt_login_login=findViewById(R.id.bt_login_login);
    }

    private void intiListener(){
        //登录按钮监听器
        bt_login_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        //注册按钮监听器
        bt_login_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }

    private void register(){
        String registerName=et_login_name.getText().toString();
        String registerPwd=et_login_pwd.getText().toString();

        //校验是否为空
        if(registerName.isEmpty()||registerPwd.isEmpty()){
            Toast.makeText(LoginActivity.this,"输入的用户名或者密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else{
            //建立线程，去服务器注册账号
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().createAccount(registerName,registerPwd);
                        //更新页面显示
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this,"注册失败"+e.toString(),Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }
    }


    private void login(){
        String loginName=et_login_name.getText().toString();
        String loginPwd=et_login_pwd.getText().toString();

        if(loginName.isEmpty()||loginPwd.isEmpty()){
            Toast.makeText(LoginActivity.this,"输入的用户名或者密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else {
            //建立线程池
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    EMClient.getInstance().login(loginName,loginPwd,new EMCallBack() {//回调

                        //登陆成功
                        @Override
                        public void onSuccess() {
                            //以下两个方法是为了保证进入主页面后本地会话和群组都 load 完毕。
                            //另外如果之前登录过，APP 长期在后台再进的时候也可能会导致加载到内存的群组和会话为空，可以在主页面的 oncreate 里也加上这两句代码
                            EMClient.getInstance().groupManager().loadAllGroups();
                            EMClient.getInstance().chatManager().loadAllConversations();
                            Log.d("main", "登录聊天服务器成功！");

                            //登陆成功后对模型层数据的处理
                            Model.getInstance().loginSuccess(new UserInfo(loginName));

                            //保存账号信息到本地数据库
                            Model.getInstance().getUserAccountDao().addAccount(new UserInfo(loginName));
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //提示登陆成功
                                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                                    //跳转到主页面
                                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }

                        //登录失败
                        @Override
                        public void onProgress(int progress, String status) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this,"登陆失败"+status,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        //登录中
                        @Override
                        public void onError(int code, String message) {
                            Log.d("main", "登录聊天服务器失败！");

                        }
                    });
                }
            });
        }
    }


}